package com.lg.edu.service.impl;

import com.lg.edu.config.EmailNotifierConfig;
import com.lg.edu.pojo.mail.EmailSymbol;
import com.lg.edu.pojo.mail.NotifyMsgDto;
import com.lg.edu.service.NotifierService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@Slf4j
public class EmailNotifierServiceImpl implements NotifierService {

    private static final String ERROR_DATA_PARAM_TITLE_EMPTY = "通知标题不能为空";
    private static final String ERROR_DATA_PARAM_CONTENT_EMPTY = "通知内容不能为空";

    @Autowired
    private EmailNotifier emailNotifier;

    @Autowired
    private EmailNotifierConfig emailNotifierConfig;

    @Override
    public Boolean sendMsg(String email, String code) {

        // 构建发送实体
        NotifyMsgDto notifyMsgDto = new NotifyMsgDto();
        notifyMsgDto.setTitle("TGest");
        notifyMsgDto.setSubTitle("TGestiy");
        notifyMsgDto.setContent(code);
        notifyMsgDto.setEmailAdmin(email);

        try {
            /* 判断必填参数 */
            // 标题
            if (org.apache.commons.lang.StringUtils.isBlank(notifyMsgDto.getTitle())) {
                log.info(ERROR_DATA_PARAM_TITLE_EMPTY);
                return false;
            }

            // 内容
            if (org.apache.commons.lang.StringUtils.isBlank(notifyMsgDto.getContent())) {
                log.info(ERROR_DATA_PARAM_CONTENT_EMPTY);
                return false;
            }

            // 邮件通知开启
            // 获取邮件发件人、收件人（管理员）、抄送人
            String from = emailNotifierConfig.getFrom();
            String to = (StringUtils.isNotBlank(notifyMsgDto.getEmailAdmin()))
                    ? notifyMsgDto.getEmailAdmin() : emailNotifierConfig.getTo();
            String copyTo = (notifyMsgDto.getReceivers() != null && !notifyMsgDto.getReceivers().isEmpty())
                    ? getCopyReceivers(notifyMsgDto.getReceivers(), to) : emailNotifierConfig.getCopyTo();
            // 调用方法发送邮件
            emailNotifier.sendEmail(notifyMsgDto.getTitle(), notifyMsgDto.getContent(), from, to, copyTo, null);

            System.out.println(">>>>>> 验证码[" + notifyMsgDto.getContent() + "]发送至[" + notifyMsgDto.getEmailAdmin() + "]成功！");

            return true;
        } catch (Exception e) {
            // 异常（通知错误）返回
            log.error("邮件发送消息失败！", e);
            return false;
        }

    }

    /**
     * 获取抄送人列表（需要排除管理员的邮箱）
     *
     * @param receivers
     * @param admin
     * @return
     */
    private String getCopyReceivers(List<String> receivers, String admin) {
        if (receivers != null && !receivers.isEmpty() && StringUtils.isNotBlank(admin)) {
            for (int i = receivers.size() - 1; i >= 0; i--) {
                if (receivers.get(i).equalsIgnoreCase(admin)) {
                    receivers.remove(i);
                }
            }
        }
        return String.join(EmailSymbol.SEPARATOR_COMMA, receivers);
    }

}
